﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.ProductionOrders;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.ProductionOrdersCommand;

namespace SmartProduction.Write.API.Application.CommandHandlers.ProductionOrdersCommandHandler
{
    /// <summary>
    /// 生产工单的添加命令处理方法
    /// </summary>
    public class ProductionOrdersCreateCommandHandler : IRequestHandler<ProductionOrdersCreateCommand, APIResult<object>>
    {
        /// <summary>
        /// AutoMapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 工单基本信息
        /// </summary>
        private readonly IBaseRepository<Basicinformation> basicinforRepository;
        /// <summary>
        /// 生产物品
        /// </summary>
        private readonly IBaseRepository<Productiongoods> productionRepository;
        /// <summary>
        /// 工序流程
        /// </summary>
        private readonly IBaseRepository<Processflow> processRepository;

        public ProductionOrdersCreateCommandHandler(IMapper mapper, IBaseRepository<Basicinformation> basicinforRepository, IBaseRepository<Productiongoods> productionRepository, IBaseRepository<Processflow> processRepository)
        {
            this.mapper = mapper;
            this.basicinforRepository = basicinforRepository;
            this.productionRepository = productionRepository;
            this.processRepository = processRepository;
        }

        /// <summary>
        /// 构造方法
        /// </summary>

        /// <summary>
        /// 工单添加处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<object>> Handle(ProductionOrdersCreateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            try
            {
                Basicinformation basicinfo = new Basicinformation
                {
                    WorkOrderNo = request.WorkOrderNo,
                    WorkOrderName = request.WorkOrderName,
                    EnterProductionOrderNo = request.EnterProductionOrderNo,
                    SalesOrder = request.SalesOrder,
                    AssociatedProject = request.AssociatedProject,
                    WorkOrderDate = request.WorkOrderDate,
                    PersoninCharge = request.PersoninCharge,
                    Department = request.Department,
                    Remarks = request.Remarks,
                    Attachment = request.Attachment,
                };
                var basic = await basicinforRepository.AddAsync(basicinfo);
                if (basic <= 0)
                {
                    result.Code = APIEnums.添加失败;
                    result.Msg = APIEnums.添加失败.ToString();
                    return result;
                }

                Productiongoods Production = new Productiongoods
                {
                    SelectProducedItem = request.SelectProducedItem,
                    EnterItemNo = request.EnterItemNo,
                    EnterSpecificationModel = request.EnterSpecificationModel,
                    EnterUnit = request.EnterUnit,
                    EnterItemCategory = request.EnterItemCategory,
                    EnterItemClassification = request.EnterItemClassification,
                    SelectPlannedStartTime = request.SelectPlannedStartTime,
                    SelectPlannedEndTime = request.SelectPlannedEndTime,
                    EnterPlannedOutput = request.EnterPlannedOutput,
                    WorkOrderNo = request.WorkOrderNo,
                };
                var Productioninfo = await productionRepository.AddAsync(Production);
                if (basic <= 0)
                {
                    result.Code = APIEnums.添加失败;
                    result.Msg = APIEnums.添加失败.ToString();
                    return result;
                }

                if(request.processflow!=null&&request.processflow.Any())
                {
                    foreach (var item in request.processflow)
                    {
                        item.WorkOrderNo=request.WorkOrderNo;
                    }
                    var process = await processRepository.AddRanges(request.processflow);
                    if (process <= 0)
                    {
                        result.Code = APIEnums.添加失败;
                        result.Msg = APIEnums.添加失败.ToString();
                    }
                    else
                    {
                        result.Code = APIEnums.添加成功;
                        result.Msg = APIEnums.添加成功.ToString();
                    }
                }
                else
                {
                    result.Code = APIEnums.添加失败;
                    result.Msg = APIEnums.添加失败.ToString();
                }
            }
            catch (Exception)
            {

                throw;
            }
            return await Task.FromResult(result);
        }
    }
}
